package com.ysjy.course.service.impl;

import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.common.constants.PromptMessage;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.utils.DateUtil;
import com.wkbb.common.utils.RandomUtil;
import com.wkbb.sports.dto.DeviceDto;
import com.wkbb.sports.model.HlifeDevice;
import com.ysjy.course.dto.TeacherDto;
import com.ysjy.course.mapper.TrusteeshipMapper;
import com.ysjy.course.mapper.YsTeacherMapper;
import com.ysjy.course.model.YsTeacher;
import com.ysjy.course.model.YsTrusteeship;
import com.ysjy.course.service.TrusteeshipService;
import com.ysjy.course.service.YsTeacherService;
import com.ysjy.course.utils.BeanBind;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
public class YsTeacherServiceImpl extends BaseServiceImpl<YsTeacher> implements YsTeacherService {

    @Autowired
    private YsTeacherMapper ysTeacherMapper;

    @Autowired
    public void setMapper(YsTeacherMapper mapper) {
        super.setMapper(mapper);
    }

    @Override
    public ResultDTO<Boolean> putTeacher(YsTeacher data) throws Exception {
        if (data.getMobile() != null){
            YsTeacher ysTeacher = ysTeacherMapper.getByMobile(data.getMobile());
            if (ysTeacher != null && !ysTeacher.getId().equals(data.getId())){
                return ResultDTO.errorResult("错误！该手机号用户已存在");
            }
            if (data.getEnabled() == null) data.setEnabled(true);
            data.setUpdateTime(new Date());
            if (data.getId() == null){
                if (data.getPassword() == null) data.setPassword("e10adc3949ba59abbe56e057f20f883e");
                ysTeacherMapper.insert(data);
            }else{
                ysTeacherMapper.update(data);
            }
            return ResultDTO.successResult();
        }
        return ResultDTO.errorResult(PromptMessage.INFO_IS_NULL);
    }

    @Override
    @Transactional(readOnly = true)
    public TeacherDto wxLogin(String mobile) throws Exception{
        YsTeacher data = ysTeacherMapper.getByMobile(mobile);
        if (data == null) return null;
        BeanBind bind = new BeanBind(YsTeacher.class,TeacherDto.class);
        TeacherDto dto = (TeacherDto)bind.bindBean(data);
        return dto;
    }
}
